<?php
// Set adodb cache directory
require('adodb/adodb.inc.php');
require('adodb/adodb-exceptions.inc.php');

$ADODB_CACHE_DIR = 'adodbcache';

class db_factory {
	// Set MySQL Connection Variables
	private $_db;
	private $_dbType = "mysql";
	private $_dbHost = "127.0.0.1";
	private $_dbUser = "root";
	private $_dbPass = "passw0rd";
	private $_dbName = "core_acl";
	private $_SQLResults = "";
	private static $_instance = false;


	private function db_factory(){
		// Open MySQL Connection
		$this->_DB = &ADONewConnection($this->_dbType);
		$this->_DB->Connect($this->_dbHost, $this->_dbUser, $this->_dbPass, $this->_dbName);
		//echo $this->_DB->ErrorMsg();
	}
	
	public static function GetInstance()
	{
		if(self::$_instance == false)
		{
			self::$_instance = new db_factory();
			
		}
			return self::$_instance;
	}

	public function ExecuteQuery($strQuery){
		//Execut Query - SELECT")
		#$this->_SQLResults = $this->_DB->GetAll($strQuery);
		$this->_SQLResults = $this->_DB->getAssoc($strQuery);
		return $this->_SQLResults;
	}

	function ExecuteCacheQuery($strQuery){
		//Execut Cached Query - SELECT
		$this->_SQLResults = $this->_DB->CacheGetAll($strQuery);
		return $this->_SQLResults;
	}

	function ExecuteNonQuery($strQuery){
		//Execute Non Query - DELETE, UPDATE and INSERT	
		return $this->_DB->execute($strQuery); 
	}

	function GetLastInsertedID(){
		//Returns Last Inserted ID
		return $this->_DB->Insert_Id();
		}

	function GetRowCount(){
		//Returns row count
		return count($this->_SQLResults);
		}

	function GetResults(){
		//Returns results
		return $this->_SQLResults;
		}

	function GetRowAffected(){
		//Returns rows affected
		return $this->_DB->AffectedRows();
	}

	function Close(){
		//Closes connection
		$this->_DB->Close();
	}
}//End DatabaseManager
?>
